<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\FreightModel;
use app\admin\model\FreightTplModel;
use app\admin\model\LogisticsModel;
use app\shop\model\OrderModel;
use app\shop\model\ShippingModel;
use cmf\controller\AdminBaseController;
use app\admin\model\GoodCategoryModel;
use app\admin\model\GoodsModel;
use think\Db;
use app\admin\model\RouteModel;
use app\admin\model\ThemeModel;


class OrderController extends AdminBaseController
{
    /**
     * 订单管理
     * @adminMenu(
     *     'name'   => '订单管理',
     *     'parent' => 'admin/Stock/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 30,
     *     'icon'   => '',
     *     'remark' => '订单管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $order=new OrderModel();
        $order_list=$order->get_order_list($param);

        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('order_no', isset($param['order_no']) ? $param['order_no'] : '');
        $this->assign('order_status', isset($param['order_status']) ? $param['order_status'] : -2);
        $order_list->appends($param);
        $this->assign('order_list', $order_list->items());
        $this->assign('page', $order_list->render());
        return $this->fetch();
    }
    /**
     * 订单详情
     * @adminMenu(
     *     'name'   => '订单详情',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 30,
     *     'icon'   => '',
     *     'remark' => '订单详情',
     *     'param'  => ''
     * )
     */
    public function order_info(){
        $param=request()->param();
        $id=$param['order_id'];
        $order=new OrderModel();
        $Logistics=new LogisticsModel();
        $orders_info=$order->get_order_detail($id);	
        if($orders_info['shipping']==null&&$orders_info['order_status']==2){
            $shipping_info=Db::name('logistics')->where('id',$orders_info['shipping_company_id'])->find();
            $data['shippercode']=$shipping_info['logistics_100code'];
            $data['logisticcode']=$orders_info['shipping_code'];
            $rs=hook_one('get_shipping_info_juhe',$data);  
            $info['order_id']=$id;
            $info['shipping_code']=$orders_info['shipping_code'];
            $info['shipping_company']=$shipping_info['logistics_code'];
            $info['shipping_info']=$rs;
            $orders_info['shipping']=$info;
            Db::name('shipping')->insert($info);
        }

        $Logistics_list=$Logistics->get_list();
        $this->assign('orders_info',$orders_info);
        $this->assign('Logistics_list',$Logistics_list);
        return $this->fetch('orders_info');
    }


    public function logistics_edit(){
        $order=new OrderModel();
        $param=request()->param();
        $order_id=$param['order_id'];
        $rs=$order->where('order_id',$order_id)->find();

        if($rs['order_status']==1||$rs['order_status']==2){
            if($rs['shipping_code']){
                Db::name('shipping')->where('shipping_code',$rs['shipping_code'])->delete();
            }
            unset($param['order_id']);
            $param['order_status']=2;
            $param['shipping_status']=1;
            $param['consign_time']=time();
            $order->where('order_id',$order_id)->update($param);
            $this->success('发货成功');
        }else{
            $this->success('当前订单状态不准确，请先刷新');
        }


    }
    public function logistics_edit2(){
        $order=new OrderModel();
        $param=request()->param();
        $order_id=$param['order_id'];
        $rs=$order->where('order_id',$order_id)->find();

        if($rs['order_status']==9||$rs['order_status']==10){
            unset($param['order_id']);
            $param['order_status']=10;
            $param['refund_storefahuo_time']=time();
            $order->where('order_id',$order_id)->update($param);
            $this->success('发货成功');
        }else{
            $this->success('当前订单状态不准确，请先刷新');
        }

    }

    function update_shipping(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $shipping=new ShippingModel();
        $shipping->update_shipping($order_id);
        $this->success('更新成功');
    }

    function edit_status(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $order=new OrderModel();
        if(isset($param['refund_no_reason'])){
            $info['order_status']=7;
            $info['refund_no_reason']=$param['refund_no_reason'];
        }else{
            $info['order_status']=6;
        }
        $order->where('order_id',$order_id)->update($info);
        $this->success('更新成功');
    }

    function edit_status_get(){
        $param=request()->param();
        $order_id=$param['order_id'];
        $order=new OrderModel();
        $orders_info=$order->get_order_detail($order_id);
        if($orders_info['refund_type']==1){
            $return_info=Db::name('order_return')->where('order_id',$order_id)->find();
          
            if($return_info==null){
                $data['return_order_no']=cmf_get_order_sn();
                $data['order_id']=$order_id;
                $data['transaction_id']=$orders_info['out_trade_no'];
                $data['status']=0;
                $data['return_price']=$orders_info['refund_require_money'];
                $data['return_score']=$orders_info['refund_require_score'];
                $data['count_price']=$orders_info['pay_money'];
                $return_id=Db::name('order_return')->insertGetId($data);
                $return_info=Db::name('order_return')->where('return_id',$return_id)->find();
           
            }else{
                $data['return_order_no']=cmf_get_order_sn();
                //更新退款交易号
                Db::name('order_return')->where('order_id',$order_id)->update($data);
                $return_info=Db::name('order_return')->where('order_id',$order_id)->find();
            }
            $info['transaction_id']=$return_info['transaction_id'];
            $info['total_fee']=$return_info['count_price'];
            $info['refund_fee']=$return_info['return_price'];
            $info['return_order_no']=$return_info['return_order_no'];
           
            $rs=hook('refundweixin',$info);
         	file_put_contents('1.txt',var_export($rs,true),FILE_APPEND);
            if($rs[0]['result_code']=='FAIL'){
                $this->error($rs['err_code_des']);
            }
            if($rs[0]['result_code']=='SUCCESS'){
                $sus['return_transaction_id']=$rs[0]['refund_id'];
                Db::name('order_return')->where('return_id',$return_id)->update($sus);
                //返还积分
                $score=$return_info['return_score'];
                $uid=$orders_info['buyer_id'];
                Db::name('user')->where('id',$uid)->setInc('score', $score);
                //添加记录
                $u['user_id']=$uid;
                $u['create_time']=time();
                $u['action']='订单'.$orders_info['order_no'].'退款积分';
                $u['score']=$score;
                Db::name('user_score_log')->insert($u);
                //更新订单
                $new_order['order_status']=12;
                $new_order['refund_storeget_time']=time();

                Db::name('order')->where('order_id',$order_id)->update($new_order);
                $this->success('退款成功');
            }
        }else{
            $data['order_status']=9;
            $new_order['refund_storeget_time']=time();
            Db::name('order')->where('order_id',$order_id)->update($data);
            $this->success('操作成功');
        }

    }
  
      function shipping(){
        $param=request()->param();
        $code=$param['code']; 
        $this->assign('code',$code);
        return $this->fetch();
    }


}